home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 3 / Amiga Tools 3.iso / grafik / raytracing / rayshade-4.0.6.3 / inetray / rcfile.c < prev    next >
C/C++ Source or Header  |  1993-08-15  |  8KB  |  378 lines

  1. /*======================================================================
  2.                     R C F I L E . C 
  3.                     doc: Fri Mar 13 16:24:22 1992
  4.                     dlm: Tue Jul 20 17:21:05 1993
  5.                     (c) 1992 ant@ips.id.ethz.ch
  6.                     uE-Info: 18 0 T 0 0 72 10 2 8 ofnI
  7. ======================================================================*/
  8.  
  9. #include    <stdio.h>
  10. #include    <ctype.h>
  11. #include    <string.h>
  12. #include    <sys/param.h>
  13. #include    <sys/types.h>
  14. #include    <rpc/rpc.h>
  15. #include    "inetray.h"
  16. #include    "config.h"
  17. #include    "common.h"
  18.  
  19. typedef struct infoList {
  20.     struct infoList *next;            /* linked list */
  21.     char    name[MAXHOSTNAMELEN];        /* name */
  22.     int    nSvcs;                /* # of workers to params */
  23.     int    uid;                /* remote uid */
  24. } infoList;
  25.  
  26. static infoList *ignore = NULL;            /* ignore hosts */
  27. static infoList *host = NULL;            /* use hosts */
  28. static infoList *net = NULL;            /* use nets */
  29. static infoList local;                /* local params */
  30.  
  31. short    portNumber = RESULTPORT;        /* result port */
  32. int    rTimeout = RTIMEOUT;            /* non-default timeout */
  33. int    minWorkers = 0;                /* wait for at least those */
  34. int    maxWorkers = 99999;            /* ignore if more than those */
  35. int    minBlockSize = MINBLOCKSIZE;        /* from config.h */
  36. int    maxBlockSize = 99999;            /* cutoff after this */
  37. int    blocksPerServer = BLOCKSPERSERVER;    /* from config.h */
  38.  
  39. extern    int    sendrpc_timeout;        /* from sendrpc.c */
  40.  
  41. doThis(hName)                    /* do this machine? */
  42. char *hName;
  43. {
  44.     infoList    *dont;
  45.  
  46.     for (dont=ignore; dont!=NULL; dont=dont->next)
  47.         if (strcasecmp(dont->name,hName) == 0) return FALSE;
  48.     return TRUE;
  49. }
  50.  
  51. getHost(first,hName,nSvcs,uid)            /* get next to do */
  52. int first; char **hName; int *nSvcs,*uid;
  53. {
  54.     static infoList *cur;
  55.  
  56.     if (first) cur = host;            /* get current */
  57.     else cur = cur->next;
  58.     while ((cur != NULL) &&
  59.            (cur->name[0] == '\0'))
  60.         cur = cur->next;
  61.     if (cur == NULL) return FALSE;        /* end of list */
  62.     *hName = cur->name;
  63.     *nSvcs = cur->nSvcs;
  64.     *uid = cur->uid;
  65.     return TRUE;
  66. }
  67.  
  68. getNet(first,name,nSvcs,uid)            /* get next to do */
  69. int first; char **name; int *nSvcs,*uid;
  70. {
  71.     static infoList *cur;
  72.  
  73.     if (first) cur = net;            /* get current */
  74.     else cur = cur->next;
  75.     while ((cur != NULL) &&
  76.            (cur->name[0] == '\0')) 
  77.         cur = cur->next;
  78.     if (cur == NULL) return FALSE;        /* end of list */
  79.     *name = cur->name;
  80.     *nSvcs = cur->nSvcs;
  81.     *uid = cur->uid;
  82.     return TRUE;
  83. }
  84.  
  85. getLocal(name,nSvcs,uid)            /* get next to do */
  86. char **name; int *nSvcs,*uid;
  87. {
  88.     *name = local.name;
  89.     *nSvcs = local.nSvcs;
  90.     *uid = local.uid;
  91.     return TRUE;
  92. }
  93.  
  94. void addUseList(ac,av)                /* add one to use list */
  95. int ac; char *av[];
  96. {
  97.     char    buf[1024];
  98.     int    a,i,p;
  99.     void    parseUseList();
  100.     
  101.     local.next = NULL;
  102.     strcpy(local.name,"Local Net");
  103.     local.nSvcs = 0;
  104.     local.uid = geteuid();
  105.  
  106.     for (a=1,p=0; a<ac; a++) {        /* loop over args */
  107.         for (i=0; p<1023; i++) {    /* loop over chars */
  108.             if (av[a][i] != '\0')
  109.                 buf[p++] = av[a][i];
  110.             else
  111.                 break;
  112.         }
  113.         if (p<1023) buf[p++] = ' ';    /* inter arg space */
  114.     }
  115.     buf[p] = '\0';                /* end of args */
  116.     parseUseList(buf);
  117. }
  118.     
  119. /*----------------------------------------------------------------------*/
  120.  
  121. void readRc()                    /* read RCFILE */
  122. {
  123.     FILE    *fp;
  124.     char    *home,buf[1024],*sp,*getenv();
  125.     int    pos;
  126.  
  127.     local.next = NULL;            /* init local info */
  128.     strcpy(local.name,"Local Net");
  129.     local.nSvcs = 1;
  130.     local.uid = geteuid();
  131.     
  132.     fp = fopen(RCFILE,"r");            /* open file */
  133.     if (fp == NULL) {
  134.         home = getenv("HOME");
  135.         if (home == NULL) return;
  136.         sprintf(buf,"%s/%s",home,RCFILE);
  137.         fp = fopen(buf,"r");
  138.         if (fp == NULL) return;
  139.     }
  140.     while (fgets(buf,1024,fp) != NULL) {
  141.         sp = strchr(buf,'#');        /* kill comments */
  142.         if (sp != NULL) *sp = '\0';
  143.         if (isEmpty(buf)) continue;    /* select command type */
  144.         if (isUse(buf)) continue;
  145.         if (isIgnore(buf)) continue;
  146.         if (isPortNr(buf)) continue;
  147.         if (isTimeout(buf)) continue;
  148.         if (isMinWorkers(buf)) continue;
  149.         if (isMaxWorkers(buf)) continue;
  150.         if (isMinBlockSize(buf)) continue;
  151.         if (isMaxBlockSize(buf)) continue;
  152.         if (isBlocksPerServer(buf)) continue;
  153.         sp = strchr(buf,'\n');        /* kill newline */
  154.         if (sp != NULL) *sp = '\0';
  155.         fprintf(stderr,"%s: <%s> ignored!\n",RCFILE,buf);
  156.     }
  157.     sendrpc_timeout = rTimeout;
  158. }
  159.  
  160. static char *compare(s,trg)            /* special compare */
  161. char *s,*trg;
  162. {
  163.     while (*trg != '\0') {
  164.         while (isspace(*s)) s++;    /* skip spaces */
  165.         if (toupper(*s) != *trg)    /* check next char */
  166.             return NULL;
  167.         s++; trg++;
  168.     }
  169.     return s;
  170. }
  171.  
  172. static char *getToken(s,tBuf)            /* copy token */
  173. char *s,*tBuf;
  174. {
  175.     while (isspace(*s) || (*s == ',')) s++;    /* skip space */
  176.     if (*s == '\0') return NULL;        /* is there token? */
  177.     while (!isspace(*s) && (*s != ',')) 
  178.         *tBuf++ = *s++;            /* copy token */
  179.     *tBuf = '\0';
  180.     while (isspace(*s)) s++;        /* skip space */
  181.     return s;
  182. }
  183.  
  184. static void parseUseList(s)            /* use: removed */
  185. char *s;
  186. {
  187.     infoList *new;
  188.     char    *ts,numBuf[256],isLocal;
  189.     
  190.     do {
  191.         new = (infoList *)malloc(sizeof(infoList));
  192.         if (new == NULL) {
  193.             fprintf(stderr,"malloc() failed\n");
  194.             exit(1);
  195.         }
  196.         s = getToken(s,new->name);    /* get name */
  197.         if (s == NULL) {
  198.             fprintf(stderr,"Use List: missing token\n");
  199.             exit(1);
  200.         }
  201.         isLocal = FALSE;
  202.         if (isdigit(new->name[0])) {    /* net or host? */
  203.             new->next = net;
  204.             net = new;
  205.         } else if (compare(new->name,"LOCAL") == NULL) {
  206.                     new->next = host;
  207.             host = new;
  208.         } else {
  209.             isLocal = TRUE;
  210.         }
  211.         new->nSvcs = 1;
  212.         new->uid = geteuid();
  213.         while ((*s != '\0') && (*s != ',')) {    /* options */
  214.             ts = compare(s,"N=");
  215.             if (ts != NULL) {
  216.                 s = ts;    
  217.                 s = getToken(s,numBuf);
  218.                 if (s == NULL) {
  219.                     fprintf(stderr,
  220.                         "Use List: N= val missing\n");
  221.                     exit(1);
  222.                 }
  223.                 new->nSvcs = atoi(numBuf);
  224.             } else {
  225.                 s = compare(s,"UID=");
  226.                 if (s == NULL) {
  227.                     fprintf(stderr,
  228.                         "Use List: option missing\n");
  229.                     exit(1);
  230.                 }
  231.                 s = getToken(s,numBuf);
  232.                 if (s == NULL) {
  233.                     fprintf(stderr,
  234.                         "Use List: UID= val missing\n");
  235.                     exit(1);
  236.                 }
  237.                 new->uid = atoi(numBuf);
  238.             }
  239.         }
  240.         if (isLocal) {
  241.             local.nSvcs = new->nSvcs;
  242.             local.uid = new->uid;
  243.             free(new);
  244.         }
  245.     } while (*s == ',');
  246. }
  247.  
  248. /*----------------------------------------------------------------------*/
  249.  
  250. static isEmpty(s)                /* test if empty */
  251. char *s;
  252. {
  253.     while (*s != '\0') {
  254.         if (!isspace(*s)) return FALSE;
  255.         s++;
  256.     }
  257.     return TRUE;
  258. }
  259.  
  260. static isIgnore(s)                /* test if ignore host */
  261. char *s;
  262. {
  263.     infoList *new;
  264.     
  265.     s = compare(s,"IGNORE:");
  266.     if (s == NULL) return FALSE;
  267.     do {                    /* get number of tokens */
  268.         new = (infoList *)malloc(sizeof(infoList));
  269.         if (new == NULL) {
  270.             fprintf(stderr,"malloc() failed\n");
  271.             exit(1);
  272.         }
  273.         s = getToken(s,new->name);
  274.         if (s == NULL) {
  275.             fprintf(stderr,"%s: missing token\n",RCFILE);
  276.             exit(1);
  277.         }
  278.         new->next = ignore;
  279.         ignore = new;
  280.     } while (*s == ',');
  281.     return TRUE;
  282. }
  283.  
  284. static isUse(s)                    /* test if use-list */
  285. char *s;
  286. {
  287.     s = compare(s,"USE:");
  288.     if (s == NULL) return FALSE;
  289.     parseUseList(s);
  290.     return TRUE;
  291. }
  292.  
  293. static isPortNr(s)                /* test if port # */
  294. char *s;
  295. {
  296.     char numBuf[256];
  297.     
  298.     s = compare(s,"RESULTPORT:");
  299.     if (s == NULL) return FALSE;
  300.     s = getToken(s,numBuf);
  301.     portNumber = (short)atoi(numBuf);
  302.     return TRUE;
  303. }
  304.     
  305. static isTimeout(s)                /* test if timeout value */
  306. char *s;
  307. {
  308.     char numBuf[256];
  309.     
  310.     s = compare(s,"TIMEOUT:");
  311.     if (s == NULL) return FALSE;
  312.     s = getToken(s,numBuf);
  313.     rTimeout = atoi(numBuf);
  314.     return TRUE;
  315. }
  316.     
  317. static isMinWorkers(s)
  318. char *s;
  319. {
  320.     char numBuf[256];
  321.     
  322.     s = compare(s,"MINWORKERS:");
  323.     if (s == NULL) return FALSE;
  324.     s = getToken(s,numBuf);
  325.     minWorkers = atoi(numBuf);
  326.     return TRUE;
  327. }
  328.     
  329. static isMaxWorkers(s)
  330. char *s;
  331. {
  332.     char numBuf[256];
  333.     
  334.     s = compare(s,"MAXWORKERS:");
  335.     if (s == NULL) return FALSE;
  336.     s = getToken(s,numBuf);
  337.     maxWorkers = atoi(numBuf);
  338.     return TRUE;
  339. }
  340.     
  341. static isMinBlockSize(s)
  342. char *s;
  343. {
  344.     char numBuf[256];
  345.     
  346.     s = compare(s,"MINBLOCKSIZE:");
  347.     if (s == NULL) return FALSE;
  348.     s = getToken(s,numBuf);
  349.     minBlockSize = atoi(numBuf);
  350.     return TRUE;
  351. }
  352.     
  353. static isMaxBlockSize(s)
  354. char *s;
  355. {
  356.     char numBuf[256];
  357.     
  358.     s = compare(s,"MAXBLOCKSIZE:");
  359.     if (s == NULL) return FALSE;
  360.     s = getToken(s,numBuf);
  361.     maxBlockSize = atoi(numBuf);
  362.     return TRUE;
  363. }
  364.     
  365. static isBlocksPerServer(s)
  366. char *s;
  367. {
  368.     char numBuf[256];
  369.     
  370.     s = compare(s,"BLOCKSPERSERVER:");
  371.     if (s == NULL) return FALSE;
  372.     s = getToken(s,numBuf);
  373.     blocksPerServer = atoi(numBuf);
  374.     return TRUE;
  375. }
  376.     
  377.  
  378.